<?php

namespace app\user\controller;

use app\common\respond\HttpApi;
use app\user\logic\FenceLogic;

class FenceController
{
    /**
     * @Description 猪栏列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/08/26 18:07:03
     */
    public function list(HttpApi $respond)
    {
        $param = [
            'page' => request()->get('page') ?: 1,
            'page_size' => request()->get('page_size') ?: 10,
            'barn_id' => request()->get('barn_id'),
            'name' => request()->get('name'),
            'status' => request()->get('status'),
            'record_time_start' => request()->get('record_time_start'),
            'record_time_end' => request()->get('record_time_end')
        ];
        $rule = [
            'page' => '>:0',
            'page_size' => '>:0',
            'barn_id' => 'require|>:0',
            'name' => 'max:30',
            'status' => 'in:0,1',
            'record_time_start' => 'dateFormat:Y-m-d H:i:s',
            'record_time_end' => 'dateFormat:Y-m-d H:i:s'
        ];
        validate($rule)->check($param);

        $logic = new FenceLogic();
        $data = $logic->getList($param);

        return $respond->success($data);
    }

    /**
     * @Description 养殖栏(猪栏)详情/名片
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/08/28 15:56:45
     * @return null
     */
    public function detail(HttpApi $respond)
    {
        $param = [
            'id' => request()->get('id') ?: 0,
        ];
        $rule = [
            'id' => '>=:0'
        ];
        validate($rule)->check($param);

        $logic = new FenceLogic();
        $data = $logic->detail($param['id']);

        return $respond->success($data);
    }

    /**
     * @Description 详情补充
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/09/19 09:46:04
     * @return null
     */
    public function detailSupply(HttpApi $respond)
    {
        $param = [
            'id' => request()->get('id') ?: 0,
        ];
        $rule = [
            'id' => '>=:0'
        ];
        validate($rule)->check($param);

        $factoryId = request()->user_info['factory_id'];

        $logic = new FenceLogic();
        $data = $logic->detailSupply($factoryId,$param['id']);

        return $respond->success($data);
    }

    /**
     * @Description 猪栏下拉列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/09/05 16:41:45
     * @return null
     */
    public function selectList(HttpApi $respond)
    {
        $param = [
            'page' => request()->get('page') ?: 1,
            'page_size' => request()->get('page_size') ?: 10,
            'barn_id' => request()->get('barn_id'),
            'name' => request()->get('name')
        ];
        $rule = [
            'page' => 'require|>:0',
            'page_size' => 'require|>:0',
            'barn_id' => 'require|>:0',
            'name' => 'max:30'
        ];
        validate($rule)->check($param);

        $logic = new FenceLogic();
        $data = $logic->selectList($param);

        return $respond->success($data);
    }

    /**
     * @Description 猪栏的切换列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/09/05 17:01:54
     * @return null
     */
    public function switchList(HttpApi $respond)
    {
        $param = [
            'last_id' => (int) request()->get('last_id') ?: 0,
            'page_size' => (int) request()->get('page_size') ?: 10,
        ];
        $rule = [
            'last_id' => '>=:0',
            'page_size' => '>:0'
        ];
        validate($rule)->check($param);

        $logic = new FenceLogic();
        $data = $logic->getSwitchList($param);

        return $respond->success($data);
    }

    /**
     * @Description 修改栏名
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/09/20 10:58:39
     * @return null
     */
    public function edit(HttpApi $respond)
    {
        $param = [
            'id' => request()->post('id'),
            'name' => request()->post('name'),
        ];
        $rule = [
            'id' => 'require|>:0',
            'name' => 'require|max:30'
        ];
        validate($rule)->check($param);

        $factoryId = request()->user_info['factory_id'];

        $logic = new FenceLogic();
        $data = $logic->edit($factoryId,$param);

        return $respond->success([]);
    }

    /**
     * @Description 今日食谱
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/20 15:38:28
     * @return \think\response\Json
     */
    public function todayFeedPlan(HttpApi $respond)
    {
        $param = [
            'fence_id' => request()->get('fence_id')
        ];
        $rule = [
            'fence_id' => 'require|integer|>:0'
        ];
        validate($rule)->check($param);

        $logic = new FenceLogic();
        $data = $logic->todayFeedPlan($param['fence_id']);

        return $respond->success($data);
    }

    /**
     * @Description 通过食谱id获取猪栏列表
     * @author: zj
     * @param HttpApi $respond
     * @time 2024/03/27 09:46:21
     * @return \think\response\Json
     */
    public function useCookBookFence(HttpApi $respond)
    {
        $param = [
            'page' => (int)request()->get('page') ?: 1,
            'page_size' => (int)request()->get('page_size') ?: 10,
            'feed_plan_id' => request()->get('feed_plan_id')
        ];
        $rule = [
            'page' => 'require|integer|>:0',
            'page_size' => 'require|integer|>:0',
            'feed_plan_id' => 'require|integer|>:0'
        ];
        validate($rule)->check($param);

        $logic = new FenceLogic();
        $data = $logic->useCookBookBarn($param);

        return $respond->success($data);
    }

}